#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>

/* The following three helper functions make dealing with time values a bit
 * easier.  You probably don't need to modify them, but you can if you want. */
int timeval_to_msec(struct timeval *t) {
    return t->tv_sec*1000+t->tv_usec/1000;
}

void msec_to_timeval(int millis, struct timeval *out_timeval) {
    out_timeval->tv_sec = millis/1000;
    out_timeval->tv_usec = (millis%1000)*1000;
}

int current_msec() {
    struct timeval t;
    gettimeofday(&t,0);
    return timeval_to_msec(&t);
}
/* End time functions. */




int main() {

  int a = 10;
  float b = 20.0;

  fprintf(stderr, "\nPrinting an int: %d", a);
  fprintf(stderr, "\nPrinting a float: %f", b);
  fprintf(stderr, "\nPrinting an int cast to a float %f", (float)a);
  fprintf(stderr, "\nPrinting a float cast to an int %d", (int)b);
  fprintf(stderr, "\nPrinting an int and then a float %d %2f", a, b);
  fprintf(stderr, "\nPrinting a float and then an int %f %d", b, a);
  fprintf(stderr, "\n\n");

  char buf[1024];
  int readbytes;
  int totalbytes = 0;

  /* Record the start time. */
  int starttime = current_msec();
  
  /* Keep reading MAX_SEGMENT size chunks from stdin and sending them until
   * there's nothing left. */
  while((readbytes = fread(buf, 1, sizeof(buf), stdin)) != 0) {
    totalbytes += readbytes;
  }
  
  /* Record the end time. */
  int finished_msec = current_msec();
  
  fprintf(stderr,"\nFinished sending, closing socket.\n");
  
  fprintf(stderr,"\nSent %d bytesin %.4f seconds, %.2f kB/s\n",
	  totalbytes,
	  (finished_msec-starttime)/1000.0, 
	  totalbytes/(float)(finished_msec-starttime));
  
}
